package com.blueocean_health.care.management.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.SysResultByPage;
import com.blueocean_health.care.management.domain.dto.TeamListDto;
import com.blueocean_health.care.management.domain.vo.OrgAndProjectTreeParam;
import com.blueocean_health.care.management.domain.vo.OrganizationVo;
import com.blueocean_health.care.management.domain.vo.Param;
import com.blueocean_health.care.management.domain.vo.TeamVo;
import com.blueocean_health.care.management.exception.OrganizationException;
import com.blueocean_health.care.management.service.OrganizationService;
import com.github.pagehelper.PageInfo;

/**
* 组织模块controller
* @author huyanqiu
* @date 2018年3月20日 下午2:29:11
* @version 版本号：1.0
*/
@RestController
@RequestMapping(value = {"/organization"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class OrganizationController {
	
	@Autowired
	private OrganizationService organizationService;
	
	@Value("${sys.successCode}")
	private Integer SUCCESSCODE;
	@Value("${sys.failedCode}")
	private Integer FAILEDCODE;
	
	/**
	 * 创建组织
	 * @return
	 * @throws OrganizationException 
	 */
	@RequestMapping("/add")
	public SysResult add(@RequestBody OrganizationVo organizationVo) {
		if (StringUtils.isEmpty(organizationVo.getName())) {
			return SysResult.error("组织名称不能为空");
		}
		// 保存组织
		organizationService.add(organizationVo);
		return SysResult.ok("保存成功");
	}
	
	/**
	 * 编辑组织
	 * @return
	 * @throws OrganizationException 
	 */
	@RequestMapping("/modify")
	public SysResult modify(@RequestBody OrganizationVo organizationVo) {
		if (StringUtils.isEmpty(organizationVo.getName())) {
			return SysResult.error("组织名称不能为空");
		}
		if (StringUtils.isEmpty(organizationVo.getId())) {
			return SysResult.error("组织id不能为空");
		}
		// 编辑组织
		organizationService.modify(organizationVo);
		return SysResult.ok("编辑成功");
	}
	
	/**
	 * 编辑组织之前--判断是否被下级占用
	 * @return
	 * @throws OrganizationException 
	 */
	@RequestMapping("/modify-before")
	public SysResult modifyBefore(@RequestBody OrganizationVo organizationVo) {
		if (StringUtils.isEmpty(organizationVo.getName())) {
			return SysResult.error("组织名称不能为空");
		}
		if (StringUtils.isEmpty(organizationVo.getId())) {
			return SysResult.error("组织id不能为空");
		}
		// 编辑组织之前--判断是否被下级占用
		List<Map<String, String>> resultList = organizationService.modifyBefore(organizationVo);
		Map<String, Object> resultMap = new HashMap<>();
		if (resultList.isEmpty()) {
			// 没有被占用
			resultMap.put("code", SUCCESSCODE);
			resultMap.put("msg", "没有被占用");
		} else {
			// 被占用
			resultMap.put("code", FAILEDCODE);
			resultMap.put("msg", "下级组织的可用模块将自动更新");
		}
		return SysResult.ok(resultMap);
	}
	
	/**
	 * 删除组织
	 * @return
	 */
	@RequestMapping("/delete")
	public SysResult delete(@RequestBody String jsonString) {
		JSONObject jsonObject =  JSONObject.parseObject(jsonString);
	    Integer id = (Integer) jsonObject.get("id");
	    if (StringUtils.isEmpty(id)) {
	    	return SysResult.error("请求参数有误");
	    }
	    organizationService.delete(id);
		return SysResult.ok("删除成功");
	}
	
	/**
	 * 启用/禁用组织
	 * @return
	 */
	@RequestMapping("/status")
	public SysResult status(@RequestBody String jsonString) {
		JSONObject jsonObject =  JSONObject.parseObject(jsonString);
		// 组织id
		Integer id = (Integer) jsonObject.get("id");
		// 状态  1.启用  2.禁用
		Integer status = Integer.parseInt((String)jsonObject.get("status"));
		if (StringUtils.isEmpty(id, status)) {
			return SysResult.error("请求参数有误");
		}
		if (!(status==1 || status==2)) {
			return SysResult.error("参数非法");
		}
		// 启用/禁用组织
		organizationService.status(id, status);
		return SysResult.ok("启用/禁用组织操作成功");
	}
	
	/**
	 * 获取组织详情
	 * @return
	 */
	@RequestMapping("/query")
	public SysResult query(@RequestBody String jsonString) {
		JSONObject jsonObject =  JSONObject.parseObject(jsonString);
		// 组织id
		Integer id = (Integer) jsonObject.get("id");
		TeamVo teamVo = organizationService.query(id);
		return SysResult.ok(teamVo);
	}
	
	/**
	 * 获取组织列表
	 * @return
	 */
	@RequestMapping("/list")
	public SysResult list(@RequestBody TeamListDto teamListDto) {
		if (StringUtils.isEmpty(teamListDto.getParentId())) {
			return SysResult.error("请求参数有误");
		}
		PageInfo<OrganizationVo> pageInfo = organizationService.list(teamListDto);
		return SysResultByPage.ok(0, "SUCCESS", pageInfo.getList(), pageInfo.getTotal());
	}
	
	/**
	 * 当前登录人组织架构图
	 * @return
	 */
	@RequestMapping("/tree")
	public SysResult tree() {
		List<Param> treeList = organizationService.tree();
		return SysResult.ok(treeList);
	}
	
	/**
	 * 当前登录人组织和项目架构图
	 * @return
	 */
	@RequestMapping("/tree-all")
	public SysResult treeTeam() {
		List<OrgAndProjectTreeParam> list = organizationService.findAllSubOrgAndProjectByOrgId();
		return SysResult.ok(list);
	}
	
	/**
	 * 获得组织项目架构图
	 * @return
	 */
	@RequestMapping("/full-tree")
	public SysResult orgAndProjectTree() {
		return organizationService.orgAndProjectTree();
	}
	
}
